import 'package:star_author/config/app.dart';
import 'package:star_author/event_bus/global_event_bus.dart';
import 'package:star_author/event_bus/good_video_status_event.dart';
import 'package:star_author/event_bus/user_login_event.dart';
import 'package:star_author/utils/user_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class RouteUtil {
  static void checkLogin(BuildContext context, Function callback) {
    if (UserUtil.isLogin(context)) {
      //是否登陆
      callback();
    } else {
      GlobalEventBus().bus.fire(GoodsVideoStatusEvent());
      App.preRouteName = Get.currentRoute; //登陆前存放路由 用于跳转到我的页面
      onVerifyLogin();
    }
  }

  ///未登录下， 保存业务代码，登录成功去执行
  ///需注意配合 checkLoginNoContext方法 使用即可
  static Function? unLoginCallback;

  static void checkLoginNoContext(Function callback) {
    unLoginCallback = null;
    if (UserUtil.isLogin(Get.context!)) {
      callback();
    } else {
      unLoginCallback = callback;
      GlobalEventBus().bus.fire(GoodsVideoStatusEvent());
      onVerifyLogin();
    }
  }

  ///打开一键登录,打开授权页面
  ///jumpFrom LoginPage页面参数
  static void onVerifyLogin({String? jumpFrom}) async {
    GlobalEventBus().bus.fire(UserLoginEvent(false));
  }
}
